package com.atguigu.mybatis;

import com.atguigu.mybatis.dao.MajorDao;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MybatisPlusSqlSessionFactoryUtil {
    private static SqlSessionFactory sqlSessionFactory = initSqlSessionFactory();

    public static SqlSession openSession(){
        SqlSession session = sqlSessionFactory.openSession(true);
        return session;
    }
    public static SqlSessionFactory initSqlSessionFactory() {
        SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource.setDriverClass(com.mysql.jdbc.Driver.class);
        dataSource.setUrl("jdbc:mysql://localhost:3306/college_mgr?&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&rewriteBatchedStatements=true");
        dataSource.setUsername("root");
        dataSource.setPassword("123456yhj");
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("Production", transactionFactory, dataSource);
        MybatisConfiguration configuration = new MybatisConfiguration(environment);

        configuration.addMapper(MajorDao.class);
        //configuration.addMapper(TeacherMapper.class);
        configuration.setLogImpl(StdOutImpl.class);
        configuration.setDefaultEnumTypeHandler(org.apache.ibatis.type.EnumOrdinalTypeHandler.class);
        return new MybatisSqlSessionFactoryBuilder().build(configuration);
    }


}
